package com.xingu.xg.controller;

import com.xingu.xg.common.consts.ComRT;
import com.xingu.xg.common.consts.ComResult;
import com.xingu.xg.po.query.AddPeerNodeQuery;
import com.xingu.xg.po.query.AddUpDownNodeQuery;
import com.xingu.xg.po.query.CopyNodeQuery;
import com.xingu.xg.po.query.DeleteNodeQuery;
import com.xingu.xg.po.vo.PartSaveReqVO;
import com.xingu.xg.po.vo.PartTreeNode;
import com.xingu.xg.po.vo.PartUpdateReqVO;
import com.xingu.xg.service.PartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@Tag(name = "备品备件")
@RestController
@RequestMapping("/part")
@Validated
public class PartController {
    @Resource
    private PartService partService;

    @PostMapping("/create")
    @Operation(summary = "新增")
    public ComResult create(@Valid @RequestBody PartSaveReqVO reqVO) {
        partService.createPart(reqVO);
        return ComRT.success();
    }

    @PutMapping("/update")
    @Operation(summary = "修改")
    public ComResult update(@Valid @RequestBody PartUpdateReqVO reqVO) {
        partService.updatePartById(reqVO);
        return ComRT.success();
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除")
    public ComResult remove(@NotNull(message = "id不能为空") @PathVariable Long id) {
        partService.removeById(id);
        return ComRT.success();
    }

    @GetMapping("tree")
    @Operation(summary = "获取树形列表")
    public ComResult<List<PartTreeNode>> getTree() {
        List<PartTreeNode> tree = partService.getTreeList();
        return ComRT.success(tree);
    }

    @PostMapping("create-peer")
    @Operation(summary = "新增同级节点")
    public ComResult createPeer(@Valid @RequestBody AddPeerNodeQuery query) {
        partService.createPeer(query);
        return ComRT.success();
    }

    @PostMapping("create-up-down")
    @Operation(summary = "新增上下级节点")
    public ComResult createUpDown(@Valid @RequestBody AddUpDownNodeQuery query) {
        partService.createUpDown(query);
        return ComRT.success();
    }

    @PostMapping("copy-node")
    @Operation(summary = "复制节点")
    public ComResult copyNode(@Valid @RequestBody CopyNodeQuery query) {
        partService.copyNode(query);
        return ComRT.success();
    }

    @PostMapping("delete-node")
    @Operation(summary = "删除节点")
    public ComResult deleteNode(@Valid @RequestBody DeleteNodeQuery query) {
        partService.deleteNode(query);
        return ComRT.success();
    }

}
